package cpa.prowon.gui.actions;

import java.io.File;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.widgets.DirectoryDialog;

import pho.prowon.category.VisibleIndexCategory;
import cpa.prowon.Application;
import cpa.prowon.gui.MainWindow;

/**
 * Exports images to the file system by simply copying the files
 * to the file system in the current viewed categorization
 * @author Chris
 */
public class ExportToFolderAction
	extends PatchedAction
	implements ISelectionChangedListener {

	/**
	 * Constructor
	 * @param MainWindow The main window where the action is used
	 * @author Chris 
	 */
	public ExportToFolderAction(MainWindow w) {
		super(w);
	}

	/**
	 * Return the title key
	 * @return The title key
	 * @author Chris
	 */
	protected String getTitleKey() {
		return "menu_item_file_exporttofolder";
	}

	/**
	 * Return the tooltip key
	 * @return The tootip key
	 * @author Chris
	 */
	protected String getTooltipKey() {
		return "menu_item_file_exporttofolder_tooltip";
	}

	/**
	 * Return the image key
	 * @return The image key
	 * @author Chris
	 */
	protected String getImageKey() {
		return "";
	}

	/**
	 * Run the action
	 * @author Chris
	 */
	public void run() {

		// Prompt for the directory
		DirectoryDialog dialog = new DirectoryDialog (window.getShell());
		dialog.setMessage(Application.getInstance().getResourceBundle().getString("msg_selectfoldertoexportimages"));
		dialog.setText(Application.getInstance().getResourceBundle().getString("title_selectdirectory"));
		String result = dialog.open ();
		
		// Return if no directory was chosen
		if (result == null) return;

		// Export the images
		VisibleIndexCategory ic = (VisibleIndexCategory) window.getCategorizationViewerSelection().getFirstElement();
		ic.exportToFolder(new File(result));
		
	}

	/**
	 * Listen to the selection changed event in order to enable or disable the action
	 * @param SelectionChangedEvent The event that occurs when the selection changed
	 * @author Chris
	 * 
	 */
	public void selectionChanged(SelectionChangedEvent event) {

		// Get the selection and set the action enabled or disabled depending on this
		IStructuredSelection selection = window.getCategorizationViewerSelection();
		if (selection.size() != 1) {
			setEnabled(false);
		} else {
			setEnabled(true);
		}

	}
	
}
